<?php

include_once '../templateSys/SQL.php';
include_once '../templateSys/Settings.php';
include_once '../templateSys/manager_util.php';
include_once '../templateSys/Admin.php';
include_once 'AjaxRequestProcessor.php';
include_once 'FakturacniUdajeResolution.php';

//loginCheck
//checkUser();
global $link;
$link = getLink();

if (isset($_GET["updateParam"])) {
    if ($_GET["updateParam"] == "kosik") {
        ARP::getKosikPageData();
    }
    if ($_GET["updateParam"] == "kontakt") {
        ARP::getKontakt();
    }
    if ($_GET["updateParam"] == "vyhledavaniVelikosti") {
        ARP::getVyhledaniVelikosti();
    }
    if ($_GET["updateParam"] == "infoZnac") {
        ARP::getInfoZnac();
    }
    if ($_GET["updateParam"] == "tabulkaVelikosti") {
        ARP::getTabulkaVelikosti();
    }
    if ($_GET["updateParam"] == "prejitKPokladne") {
        checkUser();
        if (checkUserFakUdaje()) {
            ARP::getPrejitKPokladne();
        } else {
            ARP::prejdiNaZadaniFakUdaju();
        }
    }
    mysql_close($link);
    exit();
}


if (isset($_GET["form"])) {
    if (isset($_POST["platba"]) && $_POST["platba"] == "hotove") {
        ARP::prijmiObjednavkuHotove();
    }
    if (isset($_POST["platba"]) && $_POST["platba"] == "dobirka") {
        ARP::prijmiObjednavkuDobirka();
    }
    if (isset($_POST["platba"]) && $_POST["platba"] == "ucet") {
        ARP::prijmiObjednavkuUcet();
    }
    if (isset($_POST["formType"]) && $_POST["formType"] == "fakturacniUdaje") {
        fakturacniUdajeResolution();
    }
    if (isset($_POST["formType"]) && $_POST["formType"] == "fakturacniUdajeFull") {
        fakturacniUdajeResolutionFull();
    }
    mysql_close($link);
    exit();
}

if (isset($_GET["ajax"])) {
    echo getBundleAjaxMsg($_GET["ajax"]);
    mysql_close($link);
    exit();
}


if (isset($_GET['kosik'])) {
    checkUser();
    if (getUser() == null) {
        echo getBundleMsg("prihlaseniARegistrace");
    } else {
        if (getUser()->getRole() == "user") {
            echo '<div class="floatLeft"><img src="' . Settings::$imgsPath . 'kosik.PNG"/></div><div class="specific1 floatLeft underline">' . $GLOBALS["lang"]["nakupKosik"] . ' <span id="numberOfItemsInCart" class="bold mediumText">(' . sizeof(getUser()->getZbozi()) . ')</span></div><div class="loged" style="display: none;" loged="user"></div>';
        }
        if (getUser()->getRole() == "admin") {
            echo $GLOBALS["lang"]["nastrojeSpravce"] . '<div class="loged" style="display: none;" loged="admin">';
        }
    }
    mysql_close($link);
    exit();
}

if (isset($_GET['user'])) {
    checkUser();
    if (getUser() == null) {
        echo '';
    } else {
        echo getUser()->getEmail();
    }
    mysql_close($link);
    exit();
}

function sectionPicker() {

    $itemsPerPage = Settings::$itemsPerPage;
    $typeId[0] = mysql_real_escape_string($_GET['section']);
    $typeIdArray = getSubIds($typeId[0], 0);
    $count = 0;
    $ret = "";
    $page = 1;
    if (isset($_GET['page'])) {
        $page = mysql_real_escape_string($_GET['page']);
    }
    $bottomLimit = ($page - 1) * $itemsPerPage;
    $uperLimit = $page * $itemsPerPage - 1;
    $query = SQL::$select_items_byTypeId;
    while (sizeof($typeIdArray) > $count) {
        $query .= "typeid = " . $typeIdArray[$count] . " OR ";
        $count++;
    }
    $query = substr($query, 0, -3);
    $query .= ')AND a.name = "cena" ORDER BY data';
    $result = mq($query, $GLOBALS["link"]);
    global $margin;
    global $itemIdCurrent;
    $countItems = 0;
    $countItemTotal = 0;
    $firstCheck = true;
    while ($row = mysql_fetch_array($result)) {
        if ($bottomLimit <= $countItemTotal && $uperLimit >= $countItemTotal) {
            if (($countItems % 3) == 0) {
                if ($firstCheck) {
                    $ret .= "</div><div class=rowContainer>";
                    $firstCheck = false;
                } else {
                    $ret .= "</div><div class=rowContainer>";
                }
                $margin = "itemMarginTop";
            } else {
                $margin = "itemMarginTop itemMarginLeft";
            }
            if ($countItems < 3) {
                $margin = substr_replace($margin, "", 0, 14);
            }
            $itemIdCurrent = $row["itemId"];
            $ret .= parseTemplateAll(loadTemplateWithPath(Settings::$template_path . "eshop/itemListDiv/ItemListOne", "../"), 0, $row["itemId"], $row["typeId"]);
            $countItems++;
        }
        $countItemTotal++;
    }
    $ret .= "</div>";
    global $list;
    $list = $ret;
    global $numberOfRows;
    $numberOfRows = $countItemTotal;
    global $catName;
    $catName = getTypeNameById($typeId[0]);
    global $changePage;
    $changePage = "";
    global $changePageLink;
    global $changePagePage;
    global $changePageStyle;
    $changePageStyle = "";
    if ($countItemTotal % $itemsPerPage == 0) {
        $i = (int) ($countItemTotal / $itemsPerPage);
    } else {
        $i = (int) ($countItemTotal / $itemsPerPage) + 1;
    }
    while ($i != 0) {
        $changePageLink = "eshopScript/Ajax.php?section=$typeId[0]&page=" . $i;
        $changePagePage = $i;
        if ($i == $page) {
            $changePageStyle = "bold";
        }
        $changePage .= parseTemplate(loadTemplateWithPath(Settings::$template_path . "eshop/itemListDiv/ChangePageNumber", "../"));
        $changePageStyle = "";
        $i--;
    }
    // dalsi a predchozi
    if ($page == 1) {
        if ($countItems == $itemsPerPage) {
            $newpage = $page + 1;
            $changePageLink = "eshopScript/Ajax.php?section=$typeId[0]&page=" . $newpage;
            $changePage = parseTemplate(loadTemplateWithPath(Settings::$template_path . "eshop/itemListDiv/ChangePageNext", "../")) . $changePage;
        }
    } else {
        if ($countItems == $itemsPerPage) {
            $newpage = $page + 1;
            $changePageLink = "eshopScript/Ajax.php?section=$typeId[0]&page=" . $newpage;
            $changePage = parseTemplate(loadTemplateWithPath(Settings::$template_path . "eshop/itemListDiv/ChangePageNext", "../")) . $changePage;
        }
        $newpage = $page - 1;
        $changePageLink = "eshopScript/Ajax.php?section=$typeId[0]&page=" . $newpage;
        $changePage .= parseTemplate(loadTemplateWithPath(Settings::$template_path . "eshop/itemListDiv/ChangePagePrevious", "../"));
    }
    $ret = parseTemplate(loadTemplateWithPath(Settings::$template_path . "eshop/itemListDiv/ItemList", "../"));
    echo $ret;
    mysql_close($GLOBALS["link"]);
    exit();
}

if (isset($_GET['section'])) {
    sectionPicker();
}

function getSubIds($typeId) {
    $query = sprintf(SQL::$select_subtypeMapping_byParent, mysql_real_escape_string($typeId));
    $result = mq($query, $GLOBALS["link"]);
    $ret[0] = $typeId;
    while ($row = mysql_fetch_array($result)) {
        $ret = array_merge($ret, getSubIds($row["child"]));
    }
    return $ret;
}

if (isset($_GET["detailItemId"])) {
    checkUser();
    global $itemIdCurrent;
    $itemIdCurrent = mysql_real_escape_string($_GET["detailItemId"]);
    $ret = "";
    $ret = parseTemplateAll(loadTemplateWithPath(Settings::$template_path . "eshop/itemListDiv/ItemDetail", "../"), 0, $_GET["detailItemId"], 0);
    echo $ret;
    mysql_close($link);
    exit();
}

if (isset($_GET["barvaName"])) {
    global $loadTemplatePath;
    $loadTemplatePath = "../";
    checkUser();
    $ret = "";
    $query = sprintf(SQL::$select_recurrentLinkId, mysql_real_escape_string($_GET["barvaName"]));
    $result = mq($query, $link);
    $first = 1;
    $first2 = 1;
    $count = 0;
    while ($row = mysql_fetch_array($result)) {
        if ($first) {
            $ret .= '<a href="' . Settings::$templateSys_path . 'Image.php?size=full&id=' . $row["idlink"] . '" class = "cloud-zoom mainImageFull" rel="position: \'inside\', adjustX: 10, adjustY:-4">
            <img src="' . Settings::$templateSys_path . 'Image.php?size=medium&id=' . $row["idlink"] . '" class="mainImage" alt="" title="" />
            </a>';
            $ret .= '<div class="subImages">';
            $first = 0;
        }
        $ret .= '<div id="detail_subImageOneImage_' . $row["idlink"] . '" class="floatLeft">';
        if ($row["idlink"] != null) {
            $count++;
            if ($count == 1) {
                $ret .= '<img class="subImageNoLeftMargin subImage pointer" mainImageFull="' . Settings::$templateSys_path . 'Image.php?size=full&id=' . $row["idlink"] . '" mainImageSrc="' . Settings::$templateSys_path . 'Image.php?size=medium&id=' . $row["idlink"] . '" src="' . Settings::$templateSys_path . 'Image.php?size=small&id=' . $row["idlink"] . '"/>';
            } else {
                $ret .= '<img class="subImage pointer" mainImageFull="' . Settings::$templateSys_path . 'Image.php?size=full&id=' . $row["idlink"] . '" mainImageSrc="' . Settings::$templateSys_path . 'Image.php?size=medium&id=' . $row["idlink"] . '" src="' . Settings::$templateSys_path . 'Image.php?size=small&id=' . $row["idlink"] . '"/>';
            }
            $ret .= adminEditImage($row["idlink"], 0);
            if (!$first2) {
                $ret .= adminDeleteDynItem($row["idlink"], 0);
            } else {
                $first2 = 0;
            }
        }
        $ret .= "</div>";
    }
    $ret .= "</div>";
    if (getUser() != null && getUser()->getRole() == "admin" && $count < 4 && $count != 0) {
        $ret .= "<div class='clear'/>";
        $ret .= "<div class='addImageToColor editLink mediumBigText floatRight pointer' id_addImageToColour='" . $_GET["barvaName"] . "'><img src='templates/eshop/imgs/ico_add.JPG'/></div>";
    }
    echo $ret;
    mysql_close($link);
    exit();
}

if (isset($_GET['sectionList'])) {
    $rootLeaf = createTree();
    $next = true;
    $ret = "";
    $selected = "";
    $spacing = "";
    global $showRoot;
    $showRoot = false;
    $ret .= getLeaf($rootLeaf, $selected, $spacing);
    echo $ret;
    mysql_close($link);
    exit();
}

function getLeaf($currentLeaf, $selected, $spacing) {
    $name = $currentLeaf->getValue();
    $ret = "";
    if ($name == null) {
        if ($GLOBALS["showRoot"]) {
            $name = Settings::$mainMenu;
            $data = "Root";
            if ($selected == $name) {
                $ret = '<option value="' . $name . '" selected>' . $spacing . $data . '</option>';
            } else {
                $ret = '<option value="' . $name . '">' . $spacing . $data . '</option>';
            }
        }
    } else {
        $data = $name[1];
        $name = $name[0];
        if ($selected == $name) {
            $ret = '<option value="' . $name . '" selected>' . $spacing . $data . '</option>';
        } else {
            $ret = '<option value="' . $name . '">' . $spacing . $data . '</option>';
        }
    }
    $spacing .= "&nbsp;&nbsp;&nbsp;&nbsp;";
    $leafArray = $currentLeaf->getChilds();
    if ($leafArray != null) {
        foreach ($leafArray as $leaf) {
            $ret .= getLeaf($leaf, $selected, $spacing);
        }
    }
    return $ret;
}

function createTree() {
    $query = sprintf(SQL::$select_subtypeMapping);
    $result = mq($query, $GLOBALS["link"]);
    $rootLeaf = new Leaf(Settings::$mainMenuId);
    $bufferId = 0;
    while ($row = mysql_fetch_array($result)) {
        if ($row["child"] != 0) {
            $najdi = $rootLeaf->getChild($row["parent"]);
            if ($najdi == "not found") {
                $tempLeaf = new Leaf($row["child"], array($row["name"], $row["data"]));
                $tempLeaf->setParent($row["parent"]);
                $buffer[$bufferId] = $tempLeaf;
                $bufferId++;
            } else {
                $najdi->addChild(new Leaf($row["child"], array($row["name"], $row["data"])));
            }
        }
    }
    return fetchBuffer($buffer, $rootLeaf);
}

function fetchBuffer($buffer, $rootLeaf) {
    $bufferId = 0;
    foreach ($buffer as $leaf) {
        $parent = $leaf->getParent();
        $child = $leaf->getId();
        if ($child != 0) {
            $najdi = $rootLeaf->getChild($parent);
            if ($najdi == "not found") {
                $newBuffer[$bufferId] = $leaf;
                $bufferId++;
            } else {
                $najdi->addChild(new Leaf($row["child"], array($row["name"], $row["data"])));
            }
        }
    }
    if (count($newBuffer) > 0) {
        return fetchBuffer($newBuffer, $rootLeaf);
    } else {
        return $rootLeaf;
    }
}

if (isset($_GET['sectionListFull'])) {
    $rootLeaf = createTree();
    $next = true;
    $ret = "";
    if ($_GET['sectionListFull'] != "") {
        $selected = $_GET['sectionListFull'];
    } else {
        $selected = "";
    }
    $spacing = "";
    global $showRoot;
    $showRoot = true;
    $ret .= getLeaf($rootLeaf, $selected, $spacing);
    echo $ret;
    mysql_close($link);
    exit();
}

if (isset($_GET['colorList'])) {
    $query = sprintf(SQL::$select_ciselnik_barvy);
    $result = mq($query, $link);
    $ret = "";
    while ($row = mysql_fetch_array($result)) {
        $ret .= $row["value"] . ' <input class="colorbox" count="0" barva="' . $row["idval"] . '" type="checkbox" name="1_' . $row["idval"] . '"/><div class="file_' . $row["idval"] . '"></div><br/>';
    }
    echo $ret;
    mysql_close($link);
    exit();
}

if (isset($_GET['sizeList'])) {
    $query = sprintf(SQL::$select_ciselnik_velikosti);
    $result = mq($query, $link);
    $ret = "";
    $countSizeList = 1;
    while ($row = mysql_fetch_array($result)) {
        $tmp = $row["sortNumber"] % 100;
        $tmp = ($row["sortNumber"] - $tmp) / 100;
        if ($tmp != $countSizeList) {
            $ret .= '<div class="clear"><br/></div>';
            $countSizeList = $tmp;
        }
        $ret .= '<div class="floatLeft"> <input class="velikostListCheck" type="checkbox" name="2_' . $row["idval"] . '"/>' . $row["value"] . '&nbsp;&nbsp;&nbsp;&nbsp;</div>';
    }
    echo $ret;
    mysql_close($link);
    exit();
}

function sectionPickerWithParam($sectionId) {
    $_GET['section'] = $sectionId;
    sectionPicker();
}

if (isset($_GET['searchBySize'])) {
    echo searchBySize($_GET['searchBySize']);
    mysql_close($link);
    exit();
}

function searchBySize($size) {
    $query = sprintf(SQL::$searchItemsBySize, mysql_real_escape_string($size));
    $result = q($query);
    return parseItemsArray($result);
}

function parseItemsArray($result) {
    $ret = "";
    $countItems = 0;
    global $margin;
    global $itemIdCurrent;
    while ($row = mysql_fetch_array($result)) {
        if (($countItems % 3) == 0 && $countItems > 0) {
            $ret .= "</div>";
        }
        if (($countItems % 3) == 0) {
            $ret .= '<div class="rowContainer">';
            $margin = "itemMarginTop";
        } else {
            $margin = "itemMarginTop itemMarginLeft";
        }
        if ($countItems < 3) {
            $margin = substr_replace($margin, "", 0, 14);
        }
        $itemIdCurrent = $row["itemId"];
        $ret .= parseTemplateAll(loadTemplateWithPath(Settings::$template_path . "eshop/itemListDiv/ItemListOne", "../"), 0, $row["itemId"], $row["typeId"]);
        $countItems++;
    }
    $ret .= "</div>";
    global $list;
    $list = $ret;
    global $numberOfRows;
    $numberOfRows = $countItems;
    global $catName;
    $catName = getBundleMsg("vysledekVyhledavani");
    global $changePage;
    $changePage = "";
    $ret = parseTemplate(loadTemplateWithPath(Settings::$template_path . "eshop/itemListDiv/ItemList", "../"));
    return $ret;
}

function searchSpecialWords($words) {
    $specialWordsKluci = "";
    $specialWordsHolky = "";
    $specialWordsMiminka = "";
    $specialWordsObuv = "Ботинки, ботиночки, башмачки, пинетки, сапоги, сапожки, сандалии,
босоножки, туфли-балетки, туфли, кроссовки, макасины, тенниски,
сланцы, вьетнамки, полусапожки, сандалеты";
    $specialWordsDoplnky = "шапка, шапочка, берет, шапка-ушанка, колготки, носки, чулки, гольфы,
рейтузы, козырек, бейсболка, кепка-бейсболка, панамка, чепчик,
косынка, платок, перчатки, варежки, шарф, шаль, носки, комплект
носков, комплект гольф, комплект колготок, повязка, бант, украшения,
купальник, плавки, трусы купальные, купальный костюм
одеяло, покрывало, плед, парфюм, игрушки, пеленка, конверт, украшения";
    foreach ($words as $current) {
        if (substr_count($specialWordsKluci, $current) > 0) {
            sectionPickerWithParam(18);
        }
        if (substr_count($specialWordsMiminka, $current) > 0) {
            sectionPickerWithParam(16);
        }
        if (substr_count($specialWordsHolky, $current) > 0) {
            sectionPickerWithParam(17);
        }
        if (substr_count($specialWordsObuv, $current) > 0) {
            sectionPickerWithParam(19);
        }
        if (substr_count($specialWordsDoplnky, $current) > 0) {
            sectionPickerWithParam(20);
        }
    }
}

function checkCharUpperCase($current, $lower, $upper) {
    $query = "";
    if (substr_count($current, $lower) > 0) {
        $newCurrent = str_replace($lower, $upper, $current);
        $query .= sprintf(SQL::$searchMore, mysql_real_escape_string($newCurrent));
        $query .= " OR ";
    }
    return $query;
}

if (isset($_GET['search'])) {
    $words = explode(" ", $_GET['search']);
    $ret = "";
    $query = SQL::$searchItems;
    foreach ($words as $current) {        
            $query .= checkCharUpperCase($current, "š", "Š");         
            $query .= checkCharUpperCase($current, "ш", "Ш");         
        
        if (substr_count($current, "л") > 0) {
            $newCurrent = str_replace("л", "Л", $current);
            $query .= sprintf(SQL::$searchMore, mysql_real_escape_string($newCurrent));
            $query .= " OR ";
        }
        if (substr_count($current, "б") > 0) {
            $newCurrent = str_replace("б", "Б", $current);
            $query .= sprintf(SQL::$searchMore, mysql_real_escape_string($newCurrent));
            $query .= " OR ";
        }
        if (substr_count($current, "д") > 0) {
            $newCurrent = str_replace("д", "Д", $current);
            $query .= sprintf(SQL::$searchMore, mysql_real_escape_string($newCurrent));
            $query .= " OR ";
        }
        if (substr_count($current, "к") > 0) {
            $newCurrent = str_replace("к", "К", $current);
            $query .= sprintf(SQL::$searchMore, mysql_real_escape_string($newCurrent));
            $query .= " OR ";
        }
        if (substr_count($current, "н") > 0) {
            $newCurrent = str_replace("н", "Н", $current);
            $query .= sprintf(SQL::$searchMore, mysql_real_escape_string($newCurrent));
            $query .= " OR ";
        }
        if (substr_count($current, "í") > 0) {
            $newCurrent = str_replace("í", "&iacute;", $current);
            $query .= sprintf(SQL::$searchMore, mysql_real_escape_string($newCurrent));
            $query .= " OR ";
        }
        $query .= sprintf(SQL::$searchMore, mysql_real_escape_string($current));
        $query .= " AND ";
    }
    $query = substr($query, 0, -4);
    $query .= ") LIMIT 0,15";
    $result = mq($query, $link);
    global $margin;
    global $itemIdCurrent;
    $countItems = 0;
    while ($row = mysql_fetch_array($result)) {
        if (($countItems % 3) == 0 && $countItems > 0) {
            $ret .= "</div>";
        }
        if (($countItems % 3) == 0) {
            $ret .= '<div class="rowContainer">';
            $margin = "itemMarginTop";
        } else {
            $margin = "itemMarginTop itemMarginLeft";
        }
        if ($countItems < 3) {
            $margin = substr_replace($margin, "", 0, 14);
        }
        $itemIdCurrent = $row["itemId"];
        $ret .= parseTemplateAll(loadTemplateWithPath(Settings::$template_path . "eshop/itemListDiv/ItemListOne", "../"), 0, $row["itemId"], $row["typeId"]);
        $countItems++;
    }
    $ret .= "</div>";
    if ($countItems == 0) {
        searchSpecialWords($words);
    }
    global $list;
    $list = $ret;
    global $numberOfRows;
    $numberOfRows = $countItems;
    global $catName;
    $catName = getBundleMsg("vysledekVyhledavani");
    global $changePage;
    $changePage = "";
    $ret = parseTemplate(loadTemplateWithPath(Settings::$template_path . "eshop/itemListDiv/ItemList", "../"));
    echo $ret;
    mysql_close($link);
    exit();
}

function constructCartWithPrice() {
    $ret = "";
    $zbozi = getUser()->getZbozi();
    $first = '';
    $celkovaCena = 0;
    $isEmpty = true;
    foreach ($zbozi as $current) {
        if ($isEmpty) {
            $ret .= '<div class="bigText bold"> ' . getBundleMsg("kosik") . '</div>';
        }
        $isEmpty = false;
        $ret .= '<div class"oneItem" id="cartOverview">';
        $ret .= '<div class="goToDetail" detailItemId="' . $current->getItemId() . '" barva="' . $current->getBarva() . '" velikost="' . $current->getVelikost() . '">';
        $ret .= '<div class="cartSmall pointer floatLeft">' . $first . ' <div class="fadeItemINBig underline">';
        $ret .= '<img src="' . Settings::$templateSys_path . 'Image.php?size=small&id=' . $current->getObrazek() . '" />';
        $ret .= '</div>';
        $ret .= '</div>';
        $ret .= '<div class="cartBig pointer floatLeft">' . $first . ' <div class="fadeItemIN underline">';
        $ret .= $current->getNadpis();
        $ret .= '</div>';
        $ret .= '</div>';
        $ret .= '<div class="cartSmall pointer floatLeft">' . $first . ' <div class="fadeItemIN underline">';
        $ret .= $current->getMnozstvi();
        $ret .= ' KS</div>';
        $ret .= '</div>';
        $ret .= '<div class="cartSmall pointer floatLeft"> ' . $first . '<div class="fadeItemIN underline">';
        $ret .= $current->getVelikostText();
        $ret .= '</div>';
        $ret .= '</div>';
        $ret .= '</div>';
        $ret .= '<div class="cartMedium pointer floatLeft kosik_odstranit kosik_refresh" barva="' . $current->getBarva() . '" velikost="' . $current->getVelikost() . '" amount="' . $current->getMnozstvi() . '" odstranit_itemId="' . $current->getItemId() . '">' . $first . ' <div class="fadeItemIN">';
        $ret .= '<div class="specific1 underline floatLeft">' . getBundleMsg("Odstranit") . '</div><div class="floatLeft"> <img src="' . Settings::$imgsPath . 'cross.PNG"/></div>';
        $ret .= '</div>';
        $ret .= '</div>';
        $ret .= '<div class="cartSmallNoLine floatLeft">' . $first . ' <div class="fadeItemIN">';
        $ret .= $current->getCena() * $current->getMnozstvi();
        $ret .= ' ,-</div>';
        $ret .= '</div>';
        $ret .= '</div>';
        $first = '<div class="fadeItemDivider"></div>';
        $celkovaCena = $celkovaCena + $current->getCena() * $current->getMnozstvi();
    }
    if ($isEmpty) {
        $ret .= '<div class="bigText bold"> ' . getBundleMsg("kosikJePrazdny") . '</div>';
    } else {
        getUser()->setCenaCelk($celkovaCena);
        $ret .= '<div id="celkovaCenaDiv">' . getBundleMsg("celkovaCenaJe") . "<b>" . $celkovaCena . "</b>" . getBundleMsg("kcSDPH");
        $ret .= '<div id="prejitKPokladneDiv" class="link"> ' . getBundleMsg("prejitKPokladne") . '</div>';
    }
    return $ret;
}

function constructCart() {
    $ret = "";
    $zbozi = getUser()->getZbozi();
    $first = '';
    foreach ($zbozi as $current) {
        $ret .= '<div class="goToDetail" detailItemId="' . $current->getItemId() . '" barva="' . $current->getBarva() . '" velikost="' . $current->getVelikost() . '">';
        $ret .= '<div class="fadeItemSmall pointer floatLeft">' . $first . ' <div class="fadeItemINBig underline">';
        $ret .= '<img src="' . Settings::$templateSys_path . 'Image.php?size=small&id=' . $current->getObrazek() . '" />';
        $ret .= '</div>';
        $ret .= '</div>';
        $ret .= '<div class="fadeItemBig pointer floatLeft">' . $first . ' <div class="fadeItemIN underline">';
        $ret .= $current->getNadpis();
        $ret .= '</div>';
        $ret .= '</div>';
        $ret .= '<div class="fadeItemSmall pointer floatLeft">' . $first . ' <div class="fadeItemIN underline">';
        $ret .= $current->getMnozstvi();
        $ret .= ' KS</div>';
        $ret .= '</div>';
        $ret .= '<div class="fadeItemSmall pointer floatLeft"> ' . $first . '<div class="fadeItemIN underline">';
        $ret .= $current->getVelikostText();
        $ret .= '</div>';
        $ret .= '</div>';
        $ret .= '</div>';
        $ret .= '<div class="fadeItemNoLine pointer floatLeft kosik_odstranit" barva="' . $current->getBarva() . '" velikost="' . $current->getVelikost() . '" amount="' . $current->getMnozstvi() . '" odstranit_itemId="' . $current->getItemId() . '">' . $first . ' <div class="fadeItemIN">';
        $ret .= '<div class="specific1 underline floatLeft">' . getBundleMsg("Odstranit") . '</div><div class="floatLeft"> <img src="' . Settings::$imgsPath . 'cross.PNG"/></div>';
        $ret .= '</div>';
        $ret .= '</div>';
        $first = '<div class="fadeItemDivider"></div>';
    }
    return $ret;
}

if (isset($_GET['cart'])) {
    checkUser();
    echo constructCart();
    mysql_close($link);
    exit();
}

if (isset($_GET['removeItemFromCart'])) {
    checkUser();
    $item = new Item();
    $item->setBarva($_GET["barva"]);
    $item->setVelikost($_GET["velikost"]);
    $item->setMnozstvi($_GET["amount"]);
    $item->setItemId($_GET["itemId"]);
    getUser()->removeZbozi($item);
    saveSession();
    mysql_close($link);
    exit();
}


if (isset($_GET['addItemToCart']) && isset($_GET["barva"]) && isset($_GET["velikost"]) && isset($_GET["amount"]) && isset($_GET["itemId"])) {
    checkUser();
    if (getUser() == null) {
        createAnonym();
    }
    $item = new Item;
    $item->setBarva($_GET["barva"]);
    $item->setVelikost($_GET["velikost"]);
    $item->setMnozstvi($_GET["amount"]);
    $item->setItemId($_GET["itemId"]);
    $query = sprintf(SQL::$select_int_byImteIdAndName, mysql_real_escape_string($_GET["itemId"]), "cena");
    $result = mq($query, $link);
    while ($row = mysql_fetch_array($result)) {
        $item->setCena($row['data']);
    }
    $query = sprintf(SQL::$select_text_byImteIdAndName, mysql_real_escape_string($_GET["itemId"]), "nadpis");
    $result = mq($query, $link);
    while ($row = mysql_fetch_array($result)) {
        if (sizeof($row['data']) > 29) {
            $item->setNadpis(substr($row['data'], 0, 29) . "...");
        } else {
            $item->setNadpis($row['data']);
        }
    }
    $query = sprintf(SQL::$select_ciselnik_velikosti_byIdVal, mysql_real_escape_string($_GET["velikost"]));
    $result = mq($query, $link);
    while ($row = mysql_fetch_array($result)) {
        $item->setVelikostText($row['value']);
    }
    $query = sprintf(SQL::$select_img_byItemIdAndBarvaIdVal, mysql_real_escape_string($_GET["barva"]));
    $result = mq($query, $link);
    while ($row = mysql_fetch_array($result)) {
        $item->setObrazek($row['idlink']);
        break;
    }

    getUser()->setZbozi($item);
    saveSession();
    mysql_close($link);
    exit();
}

if (isset($_GET["getSklad"])) {
    $itemId = $_GET["itemId"];
    $velikost = $_GET["velikost"];
    $barva = $_GET["barva"];
    $query = sprintf(SQL::$select_sklad, mysql_real_escape_string($_GET["barva"] . "_" . $_GET["velikost"]), mysql_real_escape_string($_GET["itemId"]));
    $result = mq($query, $link);
    $ret = "Zboží není na skladě.";
    while ($row = mysql_fetch_array($result)) {
        if ($row["fkname"] == 1) {
            $ret = "Zboží je na skladě.";
        }
    }
    echo $ret;
    mysql_close($link);
    exit();
}

if (isset($_GET["orderList"])) {
    checkUser();
    echo ARP::spravaObjednavekUser();
    mysql_close($link);
    exit();
}
if (isset($_GET["kontaktniUdajeZmena"])) {
    echo ARP::kontaktniUdajeZmena();
    mysql_close($link);
    exit();
}



mysql_close($link)
?>
